package com.dao;

import com.beans.MemberInfo;
import com.beans.PageInfo;
import com.jdbc.DBUtil;

import java.util.List;

public class MemberDao {

    /**
     * 多条件查询会员数量
     * @param memberNo 会员账号
     * @param beginDate 注册起始时间
     * @param endDate 注册截止时间
     * @return 成功返回1
     */
    public int getMemberCount(String memberNo, String beginDate, String endDate) {
        String sql = "select count(*) from memberInfo where 1=1 ";
        if (memberNo!=""&&memberNo!=null){
            sql += " and memberNo like '%"+memberNo+"%'";
        }
        if (beginDate!="" &&beginDate!=null){
            sql+=" and registerDate>='"+beginDate+"'";
        }
        if (endDate!="" && endDate!=null){
            sql+=" and registerDate<='"+endDate+" 23:59:59'";
        }

        System.out.println(sql);
        Long count = DBUtil.getScalar(sql);
        return new Integer(count+"");
    }

    public List<MemberInfo> getMemberList(String memberNo, String beginDate, String endDate, PageInfo page) {
        String sql="select * from memberInfo where 1=1 ";

        if(memberNo!=""&&memberNo!=null){
            sql+=" and memberNo like '%"+memberNo+"%'";
        }
        if(beginDate!="" &&beginDate!=null){
            sql+=" and registerDate>='"+beginDate+"' ";
        }
        if(endDate!="" && endDate!=null){
            sql+=" and registerDate<='"+endDate+" 23:59:59' ";
        }

        sql+=" order by registerDate desc limit ? , ? ";

        System.out.println(sql);
        return DBUtil.getList(sql, MemberInfo.class,page.getBeginRow(),page.getPageSize());
    }

    public int deleteMember(int id) {
        String sql = "delete from memberInfo where id=?";
        return DBUtil.update(sql,id);
    }

    /**
     * 通过id获取会员对象
     * @param id 会员id
     * @return 会员对象
     */
    public MemberInfo getMemberInfoById(int id) {
        String sql = "select * from memberInfo where id=?";
        return DBUtil.getSingleObj(sql,MemberInfo.class,id);
    }
}
